import 'dart:io';

import 'package:base_flutter/base_flutter.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lottie/lottie.dart';
import 'package:ls_work/init_page/splash_page.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/routers/application.dart';
import 'package:ls_work/routers/routers.dart';
import 'package:ls_work/service/sos_warning_service.dart';
import 'package:oktoast/oktoast.dart';

import 'net/HttpUtil.dart';
import 'net_configs.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SosWarningService.instance.initService();
  runApp(MyApp());
  Log.initDebug(false);
  // 透明状态栏
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle =
        SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class MyApp extends StatelessWidget {
  final Widget? home;

  MyApp({this.home}) {
    final router = FluroRouter();
    Routes.configureRoutes(router);
    Application.router = router;
    NetErrorUtils.initErrorFormat((error) => HttpUtil.formatError(error));
    // ImageLoad.initCacheProvider((url, {scale}) => CachedNetworkImageProvider(url,scale:scale));

    initUrl();
  }

  @override
  Widget build(BuildContext context) {
    LoadingViewPlugin.initGlobeLoading(loadingWidget: (context, str) {
      return Center(
          child: Lottie.asset("assets/lottie/lottie_loading.json")
              .addToContainer(width: 300, height: 300));
    });
    return OKToast(
        child: MaterialApp(
          title: '康养平台',
          navigatorKey: NavigateService.getInstance().key,

          // showPerformanceOverlay: true, //显示性能标签
          debugShowCheckedModeBanner: false,
          // here
          navigatorObservers: [FlutterSmartDialog.observer],
          // here
          builder: (context, child) {
            FlutterSmartDialog.init();
            return MediaQuery(
              data: MediaQuery.of(context).copyWith(textScaler: TextScaler.linear(1)),
              child: child!,
            );
          },
          // builder: FlutterSmartDialog.init(),
          theme: ThemeData(
              useMaterial3: false,
              primaryColor: Colours.app_main, //Colours.app_main,
              scaffoldBackgroundColor: Colors.white,
              appBarTheme: AppBarTheme(
                  centerTitle: true,
                  elevation: 0.5,
                  color: Colors.white,
                  iconTheme: IconThemeData(color: Colors.black),
                  titleTextStyle: TextStyle(
                      color: Colors.black,
                      fontSize: 21,
                      fontWeight: FontWeight.bold))),
          home: home ?? SplashPage(),
          onGenerateRoute: Application.router?.generator,
          localizationsDelegates: [
            RefreshLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
            // GlobalEasyRefreshLocalizations.delegate,
          ],
          supportedLocales: const [
            Locale('en', ''),
            Locale('zh', 'CN'),
          ],
          localeListResolutionCallback:
              (List<Locale>? locales, Iterable<Locale> supportedLocales) {
            return locales![0];
          },
        ),
        backgroundColor: Colors.black54,
        textPadding:
            const EdgeInsets.symmetric(horizontal: 16.0, vertical: 10.0),
        radius: 20.0,
        position: ToastPosition.bottom);
  }
}
